<?php
class DangkydvsController extends AppController {

	var $name = 'Dangkydvs';
	var $components = array('Common','Session','Auth','Email', 'RequestHandler','Redirect');
    
    function beforeFilter(){ 
        parent::beforeFilter(); 
        $this->Auth->allow('view'); 
    }
    
	
	 function index() {
	 	             //$dangkydvs=$this->Dangkydv->find('list',array('conditions'=>array('docgia_id' => $this->Session->read('cur_dg'))));
	 				$this->Dangkydv->recursive = 0;
	 				$this->set('dangkydvs', $this->paginate());

	 			}
      function dvdk() {
	 	             //$dvdks = $this->Dangkydv->find('all',array('conditions'=>array('docgia_id' => $this->Session->read('cur_dg') )));
	 				//$this->Dangkydv->recursive = 0;
	 				//$this->set('dvdks', $this->paginate());
                    $this->paginate=array('conditions'=>array('docgia_id' => $this->Session->read('cur_dg')),'limit'=>10);
                    $this->set('dkdv',$this->paginate());
	 			}
	
     /*       
   	function index() {
                $this->Dangkydv->find('list',array('conditions'=>array('docgia_id' => $this->Session->read('cur_dg'))));
				$this->Dangkydv->recursive = 0;
				$this->set('dangkydvs', $this->paginate());

			}
   
    */
    function indexad() 
    {   
		$this->Dangky->recursive = 2;
		$this->set('dangkydvs', $this->paginate());
	}
   
	function view($id = null) {
		if (!$id) {
			$this->Redirect->flashWarning(__('Không Tồn Tại', true));
			//$this->redirect(array('action' => 'index'));
		}
        //$this->Dangky->recursive = 2;
		$this->set('dangkydv', $this->Dangkydv->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Dangkydv->create();
			if ($this->Dangkydv->save($this->data)) {
				$this->Session->setFlash(__('The dangkydv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dangkydv could not be saved. Please, try again.', true));
			}
		}
		$docgias = $this->Dangkydv->Docgia->find('list');
		$dichvuks = $this->Dangkydv->Dichvuk->find('list');
        
		$this->set(compact('docgias', 'dichvuks'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dangkydv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Dangkydv->save($this->data)) {
				$this->Redirect->flashSuccess('Cập Nhật Trạng Thái Thành Công',array('action' => 'indexad'));
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Redirect->flashWarning('Có lỗi. Cập Nhật Thất Bại', array('action' => 'indexad'));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Dangkydv->read(null, $id);
		}
        //$this->Dangky->recursive = 2;
		//$docgias = $this->Dangkydv->Docgia->find('list');
		//$dichvuks = $this->Dangkydv->Dichvuk->find('list');
		//$this->set(compact('docgias', 'dichvuks'));
        //$this->Dangkydv->recursive = 2;
        $this->set('dangkydv', $this->Dangkydv->read(null, $id));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for dangkydv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Dangkydv->delete($id)) {
			$this->Session->setFlash(__('Dangkydv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Dangkydv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    function dangkydv()
		{
						//debug($this->data);
						$a = $this->Session->read('cur_dg');
                        $this->data['Dangkydv']['docgia_id'] = $a;
                        $this->data['Dangkydv']['ngaydk']= date('Y-m-d');
                        $record['Dichvuk']['dichvuk_id']=$this->data['Dangkydv']['dichvuk_id'];
                        $record['Dichvuk']['noidung']=$this->data['Dangkydv']['noidung'];
						$this->data['Dangkydv']['trangthai'] = 'Chưa chấp Nhận';
                        //$this->data['Dangkydv']['soluong'] = $this->data['Dangkydv']['sotrang'];
                        if ($this->Dangkydv->save($this->data)) {
				        $this->Redirect->flashSuccess('Đăng ký dịch vụ thành công. Vui lòng chờ đợi tin phản hồi', array('action'=>'dvdk',$this->Session->read('cur_dg')));
				        //$this->redirect(array('action' => 'index'));
			             } 
                        else {
						//debug($this->data);
                        $this->Redirect->flashWarning('Có lỗi trong quá trình đăng ký. Vui lòng kiểm tra lại !!!', array('action'=>'index',$this->Session->read('cur_dg')));
			             }  
		}
}
